﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using NCT.Framework;
using System.IO;
using System.Collections;

namespace VISS.Web.Frontend {
    /// <summary>
    /// bộ lọc các từ bậy bạ.
    /// </summary>
    public class BabWordFilter {
        private static Hashtable hsFilters = null;
        private static string strPattern = string.Empty;
        private static bool IsLoaded = false;
        private static string filename = string.Empty;

        static BabWordFilter() {
            try {
                filename = HttpContext.Current.Server.MapPath("~/tpl/filter.txt");

                LoadDataFilter();
            }
            catch {

            }
        }

        private static bool LoadDataFilter() {
            try {
                if (IsLoaded) {
                    return true;
                }

                if (!File.Exists(filename)) {
                    return false;
                }

                hsFilters = new Hashtable();
                strPattern = "";
                StreamReader sr = new StreamReader(filename);
                while (sr.Peek() >= 0) {
                    string line = sr.ReadLine();
                    string[] arr = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (arr.Length < 2) {
                        continue;
                    }
                    string key = arr[0].Trim().ToLower();
                    if (!hsFilters.ContainsKey(key)) {
                        hsFilters.Add(key, arr[1]);
                        strPattern += "|" + key;
                    }
                }
                sr.Close();
                if (strPattern.Length > 0) {
                    strPattern = strPattern.Substring(1);
                }
            }
            catch {
                return false;
            }
            IsLoaded = true;
            return true;
        }

        private static string WordScrambler(Match match) {
            if (hsFilters == null) {
                return "";
            }
            return (string)hsFilters[match.Value.ToLower()];
        }

        public static string Filter(string input) {
            LoadDataFilter();
            string str = Regex.Replace(input, strPattern, new MatchEvaluator(WordScrambler), RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
            return str;
        }

        public static bool ContainsBadWords(string input) {
            LoadDataFilter();
            MatchCollection collection = Regex.Matches(input, strPattern, RegexOptions.IgnoreCase);
            if (collection.Count > 0) {
                return true;
            }
            return false;
        }

        private static void FileFilterChanged_Handler(object source, FileSystemEventArgs e) {
            if (e.FullPath.Equals(filename)) {
                BabWordFilter.IsLoaded = false;
            }
        }
    }
}